using System;
using System.IO;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Collections;
using System.Globalization;
using System.Diagnostics;

namespace WebChart
{
	public class ChartEngine
	{
		// Fields
		private ChartInterior background = new ChartInterior();
		private ChartLine border = new ChartLine(Color.Black);
		private short bottomChartPadding = 0;
		private short chartPadding = 10;
		internal Rectangle chartRect;
		private ChartCollection charts;
		internal Rectangle controlRect;
		private GridLines gridLines;
		private bool hasChartLegend;
		private short leftChartPadding = 10;
		private ChartLegend legend = new ChartLegend();
		internal Rectangle legendRect;
		private int maxPoints = 0;
		private short padding = 10;
		private ChartInterior plotBackground = new ChartInterior();
		internal Rectangle plotRect;
		private bool renderHorizontally;
		private short rightChartPadding = 0;
		internal float scaleX;
		internal float scaleY;
		internal DateTime scatterMaxDateTime;
		internal DateTime scatterMinDateTime;
		internal float scatterMinNumber;
		private bool showTitlesOnBackground;
		private bool showXValues;
		private bool showYValues;
		private Size size;
		private ChartText title;
		private short topChartPadding = 0;
		private short topPadding = 20;
		private ChartText xaxisFont;
		private int xticksInterval = 0;
		private ChartText xtitle;
		private int xvaluesInterval = 0;
		private ChartText yaxisFont;
		private float ycustomEnd = 0f;
		private float ycustomStart = 0f;
		private ChartText ytitle;
		private string yvaluesFormat;
		private float yvaluesInterval = 0f;

		// Methods
		public ChartEngine()
		{
			StringFormat format = new StringFormat();
			format.Alignment = StringAlignment.Far;
			format.FormatFlags = StringFormatFlags.LineLimit;
			this.yaxisFont = new ChartText(format);
			this.xaxisFont = new ChartText();
		}

		private void CalculateRects()
		{
			this.controlRect = new Rectangle(0, 0, this.size.Width, this.size.Height);
			int num = this.padding * 2;
			int width = this.size.Width - num;
			int height = (this.size.Height - num) - this.topPadding;
			int x = this.padding;
			int y = this.topPadding + this.padding;
			int num6 = 2 * this.chartPadding;
			if (this.hasChartLegend)
			{
				int num7 = this.legend.Width + this.padding;
				if (this.legend.Position == LegendPosition.Right)
				{
					width -= num7;
					this.legendRect = new Rectangle((this.padding + width) + this.padding, y, this.legend.Width, height);
				}
				else if (this.legend.Position == LegendPosition.Left)
				{
					width -= num7;
					x = num + this.legend.Width;
					y = this.topPadding + this.padding;
					this.legendRect = new Rectangle(this.padding, y, this.legend.Width, height);
				}
				else if (this.legend.Position == LegendPosition.Top)
				{
					height -= num7;
					x = this.padding;
					this.legendRect = new Rectangle(x, y, width, this.legend.Width);
					y = this.legendRect.Bottom + this.padding;
				}
				else if (this.legend.Position == LegendPosition.Bottom)
				{
					height -= num7;
					this.legendRect = new Rectangle(x, (y + height) + this.padding, width, this.legend.Width);
				}
			}
			this.plotRect = new Rectangle(x, y, width, height);
			this.chartRect = new Rectangle((x + this.chartPadding) + this.leftChartPadding, (y + this.chartPadding) + this.topChartPadding, ((this.plotRect.Width - num6) - this.leftChartPadding) - this.rightChartPadding, ((this.plotRect.Height - num6) - this.topChartPadding) - this.bottomChartPadding);
		}

		private Bitmap CreateBitmap()
		{
			if (this.size.Width <= 0)
			{
				this.size.Width = 1;
			}
			if (this.size.Height <= 0)
			{
				this.size.Height = 1;
			}
			return new Bitmap(this.size.Width, this.size.Height, PixelFormat.Format24bppRgb);
		}

		private void DetermineScale(IEnumerator enumCharts, float width, float height, out Chart maxPointsChart, out float minPointValue, out float maxPointValue)
		{
			maxPointsChart = null;
			enumCharts.Reset();
			this.maxPoints = -2147483648;
			float maxValue = float.MaxValue;
			float minValue = float.MinValue;
			int min = 0;
			int max = 0;
			while (enumCharts.MoveNext())
			{
				Chart current = (Chart) enumCharts.Current;
				if (current.Data != null)
				{
					float num6;
					float mean;
					current.GetMinMaxXValue(out min, out max);
					if (max > this.maxPoints)
					{
						maxPointsChart = current;
						this.maxPoints = max;
					}
					float num5 = 0f;
					current.GetMinMaxMeanValue(out num6, out num5, out mean);
					if (num6 < maxValue)
					{
						maxValue = num6;
					}
					if (num5 > minValue)
					{
						minValue = num5;
					}
				}
			}
			if (maxPointsChart == null)
			{
				this.showXValues = false;
			}
			if (this.maxPoints <= 0)
			{
				this.maxPoints = 1;
			}
			this.scaleX = width / ((float) this.maxPoints);
			if (this.ycustomEnd != 0f)
			{
				minValue = this.YCustomEnd;
			}
			if (this.ycustomStart != 0f)
			{
				maxValue = this.ycustomStart;
			}
			else if (maxValue > 0f)
			{
				maxValue = 0f;
			}
			if (minValue == maxValue)
			{
				if (minValue > 0f)
				{
					maxValue = 0f;
				}
				if (minValue < 0f)
				{
					minValue = 0f;
				}
				if (minValue == 0f)
				{
					minValue = maxValue + 100f;
				}
			}
			if (minValue <= maxValue)
			{
				minValue = maxValue + 100f;
			}
			this.scaleY = -1f * (height / (minValue - maxValue));
			minPointValue = maxValue;
			maxPointValue = minValue;
		}

		private void DrawAxisTitles(Graphics graphics)
		{
			int width = this.padding * 2;
			if ((this.ytitle != null) && (this.ytitle.Text.Length > 0))
			{
				Rectangle layoutRectangle;
				Brush brush = new SolidBrush(this.ytitle.ForeColor);
				if (this.showTitlesOnBackground)
				{
					layoutRectangle = new Rectangle(this.plotRect.Left - this.padding, this.plotRect.Top, width, this.plotRect.Height - width);
				}
				else
				{
					layoutRectangle = new Rectangle(this.plotRect.Left, this.plotRect.Top, this.chartPadding + this.leftChartPadding, this.chartRect.Height);
				}
				graphics.DrawString(this.ytitle.Text, this.ytitle.Font, brush, layoutRectangle, this.ytitle.StringFormat);
				brush.Dispose();
			}
			if ((this.xtitle != null) && (this.xtitle.Text.Length > 0))
			{
				Rectangle rectangle2;
				Brush brush2 = new SolidBrush(this.xtitle.ForeColor);
				if (this.showTitlesOnBackground)
				{
					rectangle2 = new Rectangle(this.plotRect.Left, this.plotRect.Bottom, this.plotRect.Width, width);
				}
				else
				{
					rectangle2 = new Rectangle(this.chartRect.Left, this.chartRect.Bottom, this.chartRect.Width, this.chartPadding);
				}
				graphics.DrawString(this.xtitle.Text, this.xtitle.Font, brush2, rectangle2, this.xtitle.StringFormat);
				brush2.Dispose();
			}
		}

		private void DrawBackground(Graphics graphics)
		{
			graphics.Clear(Color.White);
			Brush brush = this.background.GetBrush(graphics);
			graphics.FillRectangle(brush, this.controlRect);
			brush.Dispose();
		}

		private void DrawBorder(Graphics graphics, Pen pen, Rectangle rectChart)
		{
			graphics.DrawRectangle(pen, rectChart);
		}

		private void DrawCharts(Graphics graphics, IEnumerator enumCharts)
		{
			enumCharts.Reset();
			while (enumCharts.MoveNext())
			{
				((Chart) enumCharts.Current).Render(graphics, this.chartRect.Width, this.chartRect.Height);
			}
		}

		private void DrawHorizontalBorders(Graphics graphics, Pen penBorder, Chart maxPointsChart, int width, int height, float minPoint, float maxPoint)
		{
			if (this.gridLines != GridLines.None)
			{
				graphics.DrawLine(penBorder, (float) 0f, (float) (minPoint * this.scaleY), (float) 0f, (float) (maxPoint * this.scaleY));
				graphics.DrawLine(penBorder, (float) width, minPoint * this.scaleY, (float) width, maxPoint * this.scaleY);
				graphics.DrawLine(penBorder, 0f, maxPoint * this.scaleY, (float) width, maxPoint * this.scaleY);
			}
			if (maxPointsChart != null)
			{
				int num = -2;
				if (minPoint > 0f)
				{
					num = ((int) (minPoint * this.scaleY)) - 2;
				}
				Brush black = Brushes.Black;
				int recommendedInterval = this.RecommendedInterval;
				int num3 = (this.xticksInterval <= 0) ? 1 : this.xticksInterval;
				float num4 = minPoint * this.scaleY;
				int num5 = num3 - 1;
				for (int i = 0; i <= this.maxPoints; i += recommendedInterval)
				{
					float num7 = i * this.scaleX;
					if ((this.gridLines == GridLines.Both) || (this.gridLines == GridLines.Vertical))
					{
						graphics.DrawLine(penBorder, num7, num4, num7, num4 - height);
					}
					if (this.gridLines != GridLines.None)
					{
						num5++;
						if (num5 >= num3)
						{
							graphics.FillRectangle(black, num7 - 1f, (float) num, 2f, 4f);
							num5 = 0;
						}
					}
				}
			}
		}

		private void DrawLegend(Graphics graphics, Brush brush, IEnumerator enumCharts)
		{
			graphics.ResetTransform();
			Brush brush2 = this.legend.Background.GetBrush(graphics);
			graphics.FillRectangle(brush2, this.legendRect);
			brush2.Dispose();
			Pen pen = this.legend.Border.GetPen(graphics);
			graphics.DrawRectangle(pen, this.legendRect);
			pen.Dispose();
			graphics.SetClip(this.legendRect, CombineMode.Replace);
			graphics.TranslateTransform((float) this.legendRect.Left, (float) this.legendRect.Top);
			int startY = 10;
			Font font = this.legend.Font;
			int glyphHeight = (int) (graphics.MeasureString("CAM", font, 0).Height * 0.85f);
			int num3 = 7;
			if ((this.legend.Position == LegendPosition.Right) || (this.legend.Position == LegendPosition.Left))
			{
				int width = (this.legend.Width - 0x19) - 10;
				enumCharts.Reset();
				while (enumCharts.MoveNext())
				{
					Chart current = (Chart) enumCharts.Current;
					if (current.showLegend)
					{
						if (!(current is PieChart))
						{
							string legend = current.Legend;
							SizeF textSize = graphics.MeasureString(legend, font, width, StringFormat.GenericDefault);
							Brush glyphBrush = current.Fill.GetBrush(graphics);
							Pen glyphPen = current.Line.GetPen(graphics);
							this.DrawLegendItem(graphics, legend, glyphBrush, glyphPen, 10, glyphHeight, font, brush, startY, textSize, StringFormat.GenericDefault);
							startY += ((textSize.Height > glyphHeight) ? ((int) textSize.Height) : glyphHeight) + num3;
							glyphBrush.Dispose();
							glyphPen.Dispose();
						}
						else
						{
							PieChart chart2 = (PieChart) current;
							Color[] colors = chart2.Colors;
							int num5 = 0;
							int num6 = colors.Length - 1;
							Pen pen3 = current.Line.GetPen(graphics);
							foreach (ChartPoint point in chart2.Data)
							{
								if (num5 > num6)
								{
									num5 = 0;
								}
								Color color = colors[num5++];
								string text = point.XValue;
								SizeF ef3 = graphics.MeasureString(text, font, width, StringFormat.GenericDefault);
								Brush brush4 = new SolidBrush(color);
								this.DrawLegendItem(graphics, text, brush4, pen3, 10, glyphHeight, font, brush, startY, ef3, StringFormat.GenericDefault);
								startY += ((ef3.Height > glyphHeight) ? ((int) ef3.Height) : glyphHeight) + num3;
								brush4.Dispose();
							}
							pen3.Dispose();
						}
					}
				}
			}
			else
			{
				int num7 = this.legend.Width / (glyphHeight + 7);
				if (num7 <= 0)
				{
					num7 = 1;
				}
				int num8 = this.GetNumberOfLegends(enumCharts) + 1;
				int num9 = (int) Math.Ceiling((double) (((float) num8) / ((float) num7)));
				if (num9 <= 0)
				{
					num9 = 1;
				}
				int num10 = this.legendRect.Width / num9;
				int num11 = 0;
				int num12 = num3;
				enumCharts.Reset();
				StringFormat format = new StringFormat();
				format.Trimming = StringTrimming.EllipsisCharacter;
				format.LineAlignment = StringAlignment.Near;
				format.Alignment = StringAlignment.Near;
				format.FormatFlags = StringFormatFlags.NoWrap;
				while (enumCharts.MoveNext())
				{
					Chart chart3 = (Chart) enumCharts.Current;
					if (chart3.showLegend)
					{
						if (!(chart3 is PieChart))
						{
							if (num11 == num9)
							{
								startY += glyphHeight + num12;
								num12 = num3;
								num11 = 0;
							}
							int glyphLeft = (num11 * num10) + 10;
							string text3 = chart3.Legend;
							SizeF ef4 = graphics.MeasureString(text3, font, (int) ((num10 - glyphHeight) - 5), format);
							if (num12 < (ef4.Height - 5f))
							{
								num12 = ((int) ef4.Height) - 5;
							}
							Brush brush5 = chart3.Fill.GetBrush(graphics);
							Pen pen4 = chart3.Line.GetPen(graphics);
							this.DrawLegendItem(graphics, text3, brush5, pen4, glyphLeft, glyphHeight, font, brush, startY, ef4, format);
							brush5.Dispose();
							pen4.Dispose();
							num11++;
						}
						else
						{
							PieChart chart4 = (PieChart) chart3;
							Color[] colorArray2 = chart4.Colors;
							int num14 = 0;
							int num15 = colorArray2.Length - 1;
							Pen pen5 = chart3.Line.GetPen(graphics);
							foreach (ChartPoint point2 in chart4.Data)
							{
								if (num14 > num15)
								{
									num14 = 0;
								}
								Color color2 = colorArray2[num14++];
								if (num11 == num9)
								{
									startY += glyphHeight + num12;
									num12 = num3;
									num11 = 0;
								}
								int num16 = (num11 * num10) + 10;
								string xValue = point2.XValue;
								SizeF ef5 = graphics.MeasureString(xValue, font, (int) ((num10 - glyphHeight) - 5), format);
								if (num12 < (ef5.Height - 5f))
								{
									num12 = ((int) ef5.Height) - 5;
								}
								Brush brush6 = new SolidBrush(color2);
								this.DrawLegendItem(graphics, xValue, brush6, pen5, num16, glyphHeight, font, brush, startY, ef5, format);
								brush6.Dispose();
								num11++;
							}
							pen5.Dispose();
						}
					}
				}
			}
		}

		private void DrawLegendItem(Graphics graphics, string legend, Brush glyphBrush, Pen glyphPen, int glyphLeft, int glyphHeight, Font fontLegend, Brush brush, int startY, SizeF textSize, StringFormat format)
		{
			graphics.FillRectangle(glyphBrush, glyphLeft, startY + 2, glyphHeight, glyphHeight);
			graphics.DrawRectangle(glyphPen, glyphLeft, startY + 1, glyphHeight, glyphHeight);
			RectangleF layoutRectangle = new RectangleF(glyphLeft + 15f, (float) startY, textSize.Width, textSize.Height);
			graphics.DrawString(legend, fontLegend, brush, layoutRectangle, format);
		}

		private void DrawPlotBackground(Graphics graphics, Rectangle rectChart)
		{
			Brush black = this.plotBackground.GetBrush(graphics);
			if (black == null)
			{
				black = Brushes.Black;
			}
			graphics.FillRectangle(black, rectChart);
			black.Dispose();
		}

		private void DrawTitle(Graphics graphics)
		{
			int num = this.padding * 2;
			Brush brush = new SolidBrush(this.title.ForeColor);
			graphics.DrawString(this.title.Text, this.title.Font, brush, new Rectangle(this.padding, this.padding, this.size.Width - num, this.size.Height - num), this.title.StringFormat);
			brush.Dispose();
		}

		private void DrawVerticalBorders(Graphics graphics, Pen penBorder, int width, float minPoint, float maxPoint, float interval)
		{
			float num = minPoint * this.scaleY;
			if (this.gridLines != GridLines.None)
			{
				graphics.DrawLine(penBorder, 0f, num, (float) width, num);
			}
			if ((this.gridLines == GridLines.Horizontal) || (this.gridLines == GridLines.Both))
			{
				if (minPoint < 0f)
				{
					for (float i = 0f; i <= maxPoint; i += interval)
					{
						num = i * this.scaleY;
						graphics.DrawLine(penBorder, -1f, num, (float) width, num);
					}
					for (float j = 0f; j >= minPoint; j -= interval)
					{
						num = j * this.scaleY;
						graphics.DrawLine(penBorder, -1f, num, (float) width, num);
					}
				}
				else
				{
					for (float k = minPoint; k <= maxPoint; k += interval)
					{
						num = k * this.scaleY;
						graphics.DrawLine(penBorder, -1f, num, (float) width, num);
					}
				}
			}
		}

		private void DrawXValues(Graphics graphics, Brush brush, Chart maxPointsChart, float minPoint)
		{
			if (maxPointsChart != null)
			{
				Font font = this.xaxisFont.Font;
				int num = (this.xvaluesInterval <= 0) ? 1 : this.xvaluesInterval;
				int num2 = num - 1;
				if (minPoint < 0f)
				{
					minPoint = 0f;
				}
				ChartPointCollection data = maxPointsChart.Data;
				float y = minPoint * this.scaleY;
				float num4 = this.scaleX * num;
				int count = data.Count;
				int num6 = Math.Max(this.maxPoints, count);
				ScatterChart chart = maxPointsChart as ScatterChart;
				if (chart != null)
				{
					num4 = Math.Min(num4, (float) ((this.Size.Width / (num6 + 1)) * num));
				}
				for (int i = 0; i <= num6; i++)
				{
					if (count > i)
					{
						num2++;
						if (num2 >= num)
						{
							float num8;
							RectangleF layoutRectangle;
							if (chart != null)
							{
								num8 = this.scaleX * (chart.GetXValueForData(data[i].xvalue) - 1f);
							}
							else
							{
								num8 = i * this.scaleX;
							}
							if (!this.renderHorizontally)
							{
								layoutRectangle = new RectangleF(new PointF(num8 - 1f, y), new SizeF(num4, (float) (this.chartPadding + this.bottomChartPadding)));
							}
							else
							{
								layoutRectangle = new RectangleF(new PointF(num8 - 1f, y), new SizeF(num4, (float) (this.chartPadding + this.leftChartPadding)));
							}
							graphics.DrawString(data[i].xvalue, font, brush, layoutRectangle, this.xaxisFont.StringFormat);
							num2 = 0;
						}
					}
				}
			}
		}

		private void DrawYValues(Graphics graphics, float minPoint, float maxPoint, float interval)
		{
			Font font = this.yaxisFont.Font;
			Brush brush = new SolidBrush(this.yaxisFont.ForeColor);
			string text = (this.yvaluesFormat == null) ? "{0:0}" : this.yvaluesFormat;
			StringFormat stringFormat = this.yaxisFont.StringFormat;
			if (minPoint < 0f)
			{
				for (float i = 0f; i <= maxPoint; i += interval)
				{
					graphics.DrawString(string.Format(text, i, CultureInfo.InvariantCulture), font, brush, -1f, i * this.scaleY, stringFormat);
				}
				for (float j = 0f; j >= minPoint; j -= interval)
				{
					graphics.DrawString(string.Format(text, j, CultureInfo.InvariantCulture), font, brush, -1f, j * this.scaleY, stringFormat);
				}
			}
			else
			{
				for (float k = minPoint; k <= maxPoint; k += interval)
				{
					graphics.DrawString(string.Format(text, k, CultureInfo.InvariantCulture), font, brush, -1f, k * this.scaleY, stringFormat);
				}
			}
			brush.Dispose();
		}

		private Bitmap GenerateBitmap()
		{
			this.scatterMinDateTime = DateTime.MaxValue;
			this.scatterMaxDateTime = DateTime.MinValue;
			this.scatterMinNumber = float.MaxValue;
			this.CalculateRects();
			Bitmap image = this.CreateBitmap();
			Graphics graphics = Graphics.FromImage(image);
			try
			{
				float minPointValue;
				float maxPointValue;
				graphics.SmoothingMode = SmoothingMode.AntiAlias;
				graphics.TextRenderingHint = TextRenderingHint.SystemDefault;
				this.DrawBackground(graphics);
				if (this.title != null)
				{
					this.DrawTitle(graphics);
				}
				this.DrawPlotBackground(graphics, this.plotRect);
				Pen pen = this.border.GetPen(graphics);
				this.DrawBorder(graphics, pen, this.plotRect);
				this.DrawAxisTitles(graphics);
				IEnumerator enumCharts = ((IEnumerable) this.charts).GetEnumerator();
				Chart maxPointsChart = null;
				if (!this.renderHorizontally)
				{
					this.DetermineScale(enumCharts, (float) this.chartRect.Width, (float) this.chartRect.Height, out maxPointsChart, out minPointValue, out maxPointValue);
				}
				else
				{
					this.DetermineScale(enumCharts, (float) this.chartRect.Height, (float) this.chartRect.Width, out maxPointsChart, out minPointValue, out maxPointValue);
				}
				Rectangle rect = this.chartRect;
				rect.Inflate(2, 2);
				graphics.SetClip(rect, CombineMode.Replace);
				float dy = this.chartRect.Bottom - (minPointValue * this.scaleY);
				if (this.renderHorizontally)
				{
					dy = this.chartRect.Top;
					graphics.RotateTransform(90f);
				}
				graphics.TranslateTransform((float) this.chartRect.X, dy, MatrixOrder.Append);
				float interval = this.yvaluesInterval;
				if (interval <= 0f)
				{
					interval = (maxPointValue - minPointValue) / 10f;
					if (interval == 0f)
					{
						interval = 1f;
					}
					if (interval < 1f)
					{
						this.yvaluesFormat = "{0:n}";
					}
				}
				if (!this.RenderHorizontally)
				{
					this.DrawVerticalBorders(graphics, pen, this.chartRect.Width, minPointValue, maxPointValue, interval);
				}
				else
				{
					this.DrawVerticalBorders(graphics, pen, this.chartRect.Height, minPointValue, maxPointValue, interval);
				}
				Brush brush = new SolidBrush(this.xaxisFont.ForeColor);
				if (!this.renderHorizontally)
				{
					this.DrawHorizontalBorders(graphics, pen, maxPointsChart, this.chartRect.Width, this.chartRect.Height, minPointValue, maxPointValue);
				}
				else
				{
					this.DrawHorizontalBorders(graphics, pen, maxPointsChart, this.chartRect.Height, this.chartRect.Width, minPointValue, maxPointValue);
				}
				this.DrawCharts(graphics, enumCharts);
				graphics.ResetClip();
				if (this.showXValues)
				{
					this.DrawXValues(graphics, brush, maxPointsChart, minPointValue);
				}
				if (this.showYValues)
				{
					this.DrawYValues(graphics, minPointValue, maxPointValue, interval);
				}
				if (this.hasChartLegend)
				{
					this.DrawLegend(graphics, brush, enumCharts);
				}
				brush.Dispose();
			}
			catch (Exception exception)
			{
				EventLog.WriteEntry("Application", exception.ToString() + "\n" + exception.StackTrace);
				throw exception;
			}
			finally
			{
				graphics.Dispose();
			}
			return image;
		}

		public Bitmap GenerateErrorBitmap(string errorMessage)
		{
			Bitmap image = this.CreateBitmap();
			Graphics graphics = Graphics.FromImage(image);
			Rectangle rect = new Rectangle(10, 10, this.size.Width - 20, this.size.Height - 20);
			graphics.FillRectangle(Brushes.White, rect);
			StringFormat format = new StringFormat();
			format.Trimming = StringTrimming.Word;
			graphics.DrawString(errorMessage, ChartText.DefaultFont, Brushes.Red, rect, format);
			graphics.Dispose();
			return image;
		}

		public Bitmap GetBitmap()
		{
			return this.GenerateBitmap();
		}

		private int GetNumberOfLegends(IEnumerator enumCharts)
		{
			int num = 0;
			enumCharts.Reset();
			while (enumCharts.MoveNext())
			{
				Chart current = (Chart) enumCharts.Current;
				if (current.ShowLegend)
				{
					if (!(current is PieChart))
					{
						num++;
					}
					else
					{
						num += current.Data.Count;
					}
				}
			}
			return num;
		}

		public void SaveToFile(string filename, ImageFormat format)
		{
			if (File.Exists(filename))
			{
				File.Delete(filename);
			}
			Bitmap bitmap = null;
			try
			{
				try
				{
					bitmap = this.GenerateBitmap();
					if (bitmap == null)
					{
						bitmap = this.GenerateErrorBitmap("The bitmap was not generated correctly, Call your administrator and tell them to verify the error in the event log.");
					}
					bitmap.Save(filename, format);
				}
				catch (Exception exception)
				{
					throw new ApplicationException("Error while saving the file:", exception);
				}
				finally
				{
					if (bitmap != null)
					{
						bitmap.Dispose();
					}
				}
			}
			catch (ApplicationException exception2)
			{
				Trace.Write("Exception Occurred:" + exception2.ToString());
				Trace.Write("StackTrace:" + exception2.StackTrace);
				this.GenerateErrorBitmap(exception2.ToString()).Save(filename, format);
			}
			bitmap = null;
		}

		// Properties
		public ChartInterior Background
		{
			get
			{
				return this.background;
			}
			set
			{
				this.background = value;
			}
		}

		public ChartLine Border
		{
			get
			{
				return this.border;
			}
			set
			{
				this.border = value;
			}
		}

		public short BottomChartPadding
		{
			get
			{
				return this.bottomChartPadding;
			}
			set
			{
				this.bottomChartPadding = value;
			}
		}

		public short ChartPadding
		{
			get
			{
				return this.chartPadding;
			}
			set
			{
				this.chartPadding = value;
			}
		}

		public ChartCollection Charts
		{
			get
			{
				return this.charts;
			}
			set
			{
				this.charts = value;
			}
		}

		public GridLines GridLines
		{
			get
			{
				return this.gridLines;
			}
			set
			{
				this.gridLines = value;
			}
		}

		public bool HasChartLegend
		{
			get
			{
				return this.hasChartLegend;
			}
			set
			{
				this.hasChartLegend = value;
			}
		}

		public short LeftChartPadding
		{
			get
			{
				return this.leftChartPadding;
			}
			set
			{
				this.leftChartPadding = value;
			}
		}

		public ChartLegend Legend
		{
			get
			{
				return this.legend;
			}
			set
			{
				this.legend = value;
			}
		}

		internal int MaxPoints
		{
			get
			{
				return this.maxPoints;
			}
		}

		public short Padding
		{
			get
			{
				return this.padding;
			}
			set
			{
				this.padding = value;
			}
		}

		public ChartInterior PlotBackground
		{
			get
			{
				return this.plotBackground;
			}
			set
			{
				this.plotBackground = value;
			}
		}

		internal int RecommendedInterval
		{
			get
			{
				int num = 1;
				if ((this.scaleX < 2f) && (this.scaleX > 0f))
				{
					num = (int) (2f / this.scaleX);
				}
				return num;
			}
		}

		public bool RenderHorizontally
		{
			get
			{
				return this.renderHorizontally;
			}
			set
			{
				this.renderHorizontally = value;
			}
		}

		public short RightChartPadding
		{
			get
			{
				return this.rightChartPadding;
			}
			set
			{
				this.rightChartPadding = value;
			}
		}

		public float ScaleX
		{
			get
			{
				return this.scaleX;
			}
		}

		public float ScaleY
		{
			get
			{
				return this.scaleY;
			}
		}

		public bool ShowTitlesOnBackground
		{
			get
			{
				return this.showTitlesOnBackground;
			}
			set
			{
				this.showTitlesOnBackground = value;
			}
		}

		public bool ShowXValues
		{
			get
			{
				return this.showXValues;
			}
			set
			{
				this.showXValues = value;
			}
		}

		public bool ShowYValues
		{
			get
			{
				return this.showYValues;
			}
			set
			{
				this.showYValues = value;
			}
		}

		public Size Size
		{
			get
			{
				return this.size;
			}
			set
			{
				this.size = value;
			}
		}

		public ChartText Title
		{
			get
			{
				return this.title;
			}
			set
			{
				this.title = value;
			}
		}

		public short TopChartPadding
		{
			get
			{
				return this.topChartPadding;
			}
			set
			{
				this.topChartPadding = value;
			}
		}

		public short TopPadding
		{
			get
			{
				return this.topPadding;
			}
			set
			{
				this.topPadding = value;
			}
		}

		public ChartText XAxisFont
		{
			get
			{
				return this.xaxisFont;
			}
			set
			{
				this.xaxisFont = value;
			}
		}

		public int XTicksInterval
		{
			get
			{
				return this.xticksInterval;
			}
			set
			{
				this.xticksInterval = value;
			}
		}

		public ChartText XTitle
		{
			get
			{
				return this.xtitle;
			}
			set
			{
				this.xtitle = value;
			}
		}

		public int XValuesInterval
		{
			get
			{
				return this.xvaluesInterval;
			}
			set
			{
				this.xvaluesInterval = value;
			}
		}

		public ChartText YAxisFont
		{
			get
			{
				return this.yaxisFont;
			}
			set
			{
				this.yaxisFont = value;
			}
		}

		public float YCustomEnd
		{
			get
			{
				return this.ycustomEnd;
			}
			set
			{
				this.ycustomEnd = value;
			}
		}

		public float YCustomStart
		{
			get
			{
				return this.ycustomStart;
			}
			set
			{
				this.ycustomStart = value;
			}
		}

		public ChartText YTitle
		{
			get
			{
				return this.ytitle;
			}
			set
			{
				this.ytitle = value;
			}
		}

		public string YValuesFormat
		{
			get
			{
				return this.yvaluesFormat;
			}
			set
			{
				this.yvaluesFormat = value;
			}
		}

		public float YValuesInterval
		{
			get
			{
				return this.yvaluesInterval;
			}
			set
			{
				this.yvaluesInterval = value;
			}
		}
	}
}
